package com.punch.oauth.service.impl;

import cn.hutool.core.util.StrUtil;
import com.punch.common.exception.BusinessException;
import com.punch.common.feign.FeignGuardianService;
import com.punch.common.feign.FeignPlatformUserService;
import com.punch.common.feign.FeignTeacherService;
import com.punch.common.feign.FeignUserService;
import com.punch.common.model.LoginUser;
import com.punch.oauth.constant.ClientConstant;
import com.punch.oauth.constant.MessageConstant;
import com.punch.oauth.exception.UaaExceptionEnum;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 用户管理业务类
 * Created by macro on 2020/6/19.
 */
@Service
public class UaaUserServiceImpl implements UserDetailsService {

    @Resource
    private FeignUserService feignUserService;

    @Resource
    private FeignTeacherService feignTeacherService;

    @Resource
    private FeignGuardianService feignGuardianService;

    @Resource
    private FeignPlatformUserService feignPlatformUserService;


    @Override
    public UserDetails loadUserByUsername(String user) throws UsernameNotFoundException {
        String[] userInfo = user.split("-");
        String mobile = userInfo[0];
        String type = userInfo[1];
        LoginUser loginUser = null;
        if (ClientConstant.WEBAPP.equals(type)) {
            loginUser = feignUserService.getUserByMobile(mobile);
            loginUser.setUsername(mobile);
        }
        if (ClientConstant.H5.equals(type)) {
            loginUser = feignTeacherService.getFeignTeacherByMobile(mobile);
            loginUser.setUsername(mobile);
        }
        //app端用户登录
        if (ClientConstant.APP.equals(type)) {
            loginUser = feignPlatformUserService.getUserByMobile(mobile);
            loginUser.setUsername(mobile);
        }
        if (loginUser.getUserId() == null) {
            loginUser = null;
            throw new BusinessException(MessageConstant.USERNAME_NOT_EXIST);
        }
        //没有设置密码
        if (StrUtil.isBlank(loginUser.getPassword())) {
            throw new BusinessException(UaaExceptionEnum.NO_SET_PASSWORD);
        }
        if (!loginUser.isEnabled()) {
            throw new DisabledException(MessageConstant.ACCOUNT_DISABLED);
        } else if (!loginUser.isAccountNonLocked()) {
            throw new LockedException(MessageConstant.ACCOUNT_LOCKED);
        } else if (!loginUser.isAccountNonExpired()) {
            throw new AccountExpiredException(MessageConstant.ACCOUNT_EXPIRED);
        } else if (!loginUser.isCredentialsNonExpired()) {
            throw new CredentialsExpiredException(MessageConstant.CREDENTIALS_EXPIRED);
        }
        //存入到redis中
        return loginUser;
    }

    /**
     * 根据opendid获取用户信息
     *
     * @param openId 用户opendId
     * @param type   0-家长 1-培训教师
     * @return
     */
    public LoginUser loadUserByOpenId(String openId, int type) {
        LoginUser loginUser = null;
        if (type == 0) {
            //首次打开、时间久了重新登录
            loginUser = feignGuardianService.getFeignGuardianByOpenId(openId);
        }
        if (type == 1) {
            loginUser = feignTeacherService.getFeignTeacherByOpenId(openId);
        }
        return loginUser;
    }
}
